package com.crm.controller.before_sale;

import com.crm.pojo.ClientInfo;
import com.crm.pojo.Competitor;
import com.crm.pojo.OfferDetail;
import com.crm.pojo.SaleChance;
import com.crm.service.before_sale.CompetitorService;
import com.crm.vo.CompetitorVO;
import com.crm.vo.OfferDetailsVO;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CompetitorController {
    @Autowired
    private CompetitorService competitorService;

    //转到竞争对手表
    @GetMapping("zjk-competitor")
    public String toCompetitorDatagrid() {
        return "before-sale/competitor";
    }

    //显示编辑竞争对手表单窗口
    @GetMapping("zjk-competitor-form")
    public String toCompetitorFrom() {
        return "before-sale/competitor-form";
    }
    //显示编辑竞争对手表单窗口
    @GetMapping("zjk-add-competitor-form")
    public String toAddCompetitorFrom() {
        return "before-sale/add-competitor-form";
    }
    //显示竞争对手详情表单（仅查看）
    @GetMapping("zjk-competitor-details")
    public String toCompetitorDetails() {
        return "before-sale/competitor-details";
    }






    //获取竞争对手分页JSON数据
    @PostMapping("getCompetitors")
    @ResponseBody
    public Map getCompetitors(int page, int rows) {
        List<CompetitorVO> list = competitorService.getCompetitorList(page, rows);
        Long total = competitorService.getCompetitorExistTotal();
        Map map = new HashMap();
        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    /**
     *添加竞争对手
     * @param comName
     * @param comPower
     * @param comProduct
     * @param comPrice
     * @param coping
     * @param remarks
     * @param chId
     * @param cid
     * @return
     */
    @PostMapping("add-competitor")
    @ResponseBody
    public Result addCompetitor(String comName, String comPower, String comProduct, BigDecimal comPrice, String coping,
                                String remarks,BigDecimal chId,BigDecimal cid ){
        try {
            Result result = competitorService.addCompetitor(comName,comPower,comProduct,comPrice,coping,remarks,chId,cid);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(e);
        }
    }

    /**
     * 删除竞争对手
     *
     * @param comId
     * @return
     */
    @PostMapping("deleteCompetitor")
    @ResponseBody
    public Result deleteCompetitor(String comId) {
        Long id = comId == null ? 0 : new Long(comId);
        return competitorService.deleteCompetitor(id);

    }

    /**
     * 编辑
     * @param competitor
     * @return
     */
    @PostMapping("editCompetitor")
    @ResponseBody
    public Result editCompetitor(Competitor competitor){
        return competitorService.editCompetitor(competitor);
    }


    /**
     * 根据id获取销售机会（未删除的）
     *
     * @param chId
     * @return
     */
    @GetMapping("getSaleChanceNameByid")
    @ResponseBody
    public List<String> findAllSaleChanceNameByChId(Long chId) {
        return competitorService.findAllSaleChanceNameByChId(chId);
    }

    /**
     * 获取客户名称（未删除的）
     *
     * @return
     */
    @PostMapping("getClientInfoName")
    @ResponseBody
    public List<ClientInfo> findAllClientInfoName() {
        return competitorService.findAllClientInfoName();

    }

    /**
     * 获取销售机会（未删除的）
     *
     * @return
     */
    @PostMapping("getSaleChanceName")
    @ResponseBody
    public List<SaleChance> findAllSaleChanceName() {
        return competitorService.findAllSaleChanceName();
    }

    /**
     * 分页展示竞争对手列表
     * @param competitor
     * @param page
     * @param rows
     * @param request
     * @return
     */
    @PostMapping("showCompetitorList")
    @ResponseBody
    public Map showCompetitorList(Competitor competitor, int page, int rows, HttpServletRequest request){
        List<Competitor> list = competitorService.showCompetitorList(competitor,page,rows);
        Long total = competitorService.getCompetitorCount(competitor);
        Map map = new HashMap();
        map.put("rows",list);
        map.put("total",total);
        return  map;
    }

    /**
     * 获取竞争对手总数量
     * @param competitor
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("advance-competitor")
    @ResponseBody
    public Map advanceCompetitor(Competitor competitor,int page,int rows){
        List<Competitor> list = competitorService.showCompetitorList(competitor,page,rows);
        Long total = competitorService.getCompetitorCount(competitor);
        Map map = new HashMap();
        map.put("rows",list);
        map.put("total",total);
        return  map;
    }


    @GetMapping("zjkzjk")
    @ResponseBody
    public Map getOfferDetailVOListByClientId(Long clientId){
        List<OfferDetailsVO> list = competitorService.getOfferDetailVOListByClientId(clientId);

        Map map = new HashMap();
        map.put("rows",list);
        return  map;
    }


}
